package com.sy.simpleegg.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.sy.simpleegg.R
import com.sy.simpleegg.bean.GoodBean
import com.sy.simpleegg.config.Config
import com.sy.simpleegg.constant.PackageType
import com.sy.simpleegg.constant.ProductType
import com.sy.simpleegg.databinding.ItemGoodBinding
import com.sy.simpleegg.util.BigDecimalUtil

class GoodAdapter(
    private var mContext: Context,
    private var mGoodList: MutableList<GoodBean>
) : RecyclerView.Adapter<GoodAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GoodAdapter.ViewHolder {
        val binding = ItemGoodBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(binding)
    }

    @SuppressLint("NewApi", "SetTextI18n", "UseCompatLoadingForDrawables")
    override fun onBindViewHolder(holder: GoodAdapter.ViewHolder, position: Int) {
        val goodBean: GoodBean = mGoodList[position]
        holder.btnGood.text =
            "${BigDecimalUtil.getPrice(goodBean.money)}${Config.CURRENCY_SYMBOLS}" +
                    "\n${goodBean.amount}${when (Config.PRODUCT_TYPE){
                        ProductType.CURRENCY -> mContext.getString(R.string.bi)
                        ProductType.BEAD -> mContext.getString(R.string.bead)
                    }}"

        when (goodBean.goodType) {
            PackageType.NORMAL -> {
                holder.btnGood.background =
                    mContext.getDrawable(R.drawable.selector_btn_package_normal)
                holder.btnGood.setTextColor(mContext.getColorStateList(R.color.package_normal_text))
            }

            PackageType.RECHARGE -> {
                holder.btnGood.background =
                    mContext.getDrawable(R.drawable.selector_btn_package_recharge)
                holder.btnGood.setTextColor(mContext.getColorStateList(R.color.package_recharge_text))
            }

            PackageType.CASH -> {

            }
        }

        holder.btnGood.setOnClickListener {
            mOnClickListener?.onClick(goodBean)
        }
    }

    override fun getItemCount(): Int {
        return mGoodList.size
    }

    inner class ViewHolder(binding: ItemGoodBinding) : RecyclerView.ViewHolder(binding.root) {
        val btnGood: TextView = binding.btnGood
    }


    fun setData(newList: MutableList<GoodBean>?) {
        if (newList.isNullOrEmpty()) {
            return
        }
        mGoodList = newList
        notifyDataSetChanged()
    }


    interface OnItemClickListener {
        fun onClick(goodBean: GoodBean?)
    }

    private var mOnClickListener: OnItemClickListener? = null
    fun setOnItemClickListener(onItemClickListener: OnItemClickListener?) {
        mOnClickListener = onItemClickListener
    }
}